﻿CREATE PROCEDURE [dbo].[OrderLinesDelete]
(--Заказ товара у поставщика
	@Original_Id int,    
	@Original_MD datetime 
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @orgDocId int

SET NOCOUNT ON
IF (@Original_Id Is Null) OR (@Original_MD Is Null)
BEGIN
	IF @@trancount > 0 ROLLBACK TRANSACTION
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

SELECT @orgDocId = DocId FROM CD.ManageDocLines
WHERE Id = @Original_Id AND MD = @Original_MD
IF (@@rowcount <> 1)
BEGIN
	IF @@trancount > 0 ROLLBACK TRANSACTION
	SELECT @intErrCode = 2, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

	SET NOCOUNT OFF;

DELETE FROM CD.ManageDocLines WHERE Id = @Original_Id AND MD = @Original_MD;

RETURN 0